﻿using BCCommon;
using BCDto.TMS.RedPacket.RedPacket;
using BCDto.TMS.RedPacket.RedPacket.RedPacketRequest;
using BCDto.TMS.RedPacket.RedPacketSupplement;
using BCService.TMS.RedPacket.RedPacket;
using BCService.TMS.RedPacket.RedPacketSupplement;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace BCTMSRESTful.Controllers.Management.TMS.RedPacket
{
    /// <summary>
    /// 红包
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    public class RedPacketController : ManagementBaseController
    {
        private readonly IRedPacketService redPacketService;
        private readonly IRedPacketSupplementService redPacketSupplementService;

        public RedPacketController(IRedPacketService redPacketService,
            IRedPacketSupplementService redPacketSupplementService)
        {
            this.redPacketService = redPacketService;
            this.redPacketSupplementService = redPacketSupplementService;
        }

        /// <summary>
        /// 补充红包。
        /// </summary>
        /// <param name="redPacketId"></param>
        /// <param name="requestDto">红包</param>
        /// <response code="201">补充红包</response>
        /// <returns></returns>
        [HttpPost("{red_packet_id}")]
        [Log("补充红包")]
        [ProducesResponseType(typeof(RedPacketDto), 200)]
        public IActionResult Create([FromRoute(Name = "red_packet_id")] long redPacketId,
            [FromBody] RedPacketRequestDto requestDto)
        {
            var result = this.redPacketService.Create(redPacketId, requestDto, this.Admin);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取红包补充记录分页
        /// </summary>
        /// <param name="redPacketId">红包id</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<RedPacketSupplementDto>), 200)]
        public IActionResult SupplementQuery([FromQuery(Name = "red_packet_id")] long? redPacketId,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = this.redPacketSupplementService.GetList(this.Admin, redPacketId, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取红包数量
        /// </summary>
        /// <response code="201">获取红包数量</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(RedPacketCountDto), 200)]
        public IActionResult GetRedPacketCount()
        {
            var result = this.redPacketService.GetRedPacketCount(this.Admin);
            return this.Ok(result);
        }
        /// <summary>
        /// 获取红包列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<RedPacketDto>), 200)]
        public IActionResult GetList([FromQuery(Name = "enabled")] bool? enabled)
        {
            var result = this.redPacketService.GetList(enabled, this.Admin);
            return this.Ok(result);
        }

        /// <summary>
        /// 修改红包是否可用
        /// </summary>
        /// <returns></returns>
        [HttpPut("[action]/{red_packet_id}/{enabled}")]
        [ProducesResponseType(typeof(NoContentResult), 200)]
        public IActionResult Enabled([FromRoute(Name = "red_packet_id")] long redPacketId,
            [FromRoute(Name = "enabled")] bool enabled)
        {
            if (this.Admin.AgentRecord == null)
            {
                throw new ArgumentException(" 用户验证失败,错误操作 ");
            }
            if (enabled)
                redPacketService.Enabled(redPacketId, Admin);
            else
                redPacketService.Disabled(redPacketId, Admin);
            return NoContent();
        }
    }
}
